package b3d.parsers
{
	import b3d.geometry.Mesh3D;
	
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.Dictionary;

	public class Parser_DAE extends BaseParser
	{
		private static var VERTEX:String = "VERTEX";
		private static var NORMAL:String = "NORMAL";
		private static var UV:String = "TEXCOORD";
		private static var TANGENT:String = "TEXTANGENT";
		private static var BINORMAL:String = "TEXBINORMAL";
		
		private var data			:XML;
		private var currentMeshData	:MeshData;
		
		public function Parser_DAE(){}
		
		public override function parseFromUrl(url:String, onParseComplete:Function):void
		{
			this.url 				= url;
			this.onParseComplete 	= onParseComplete;
			var loader:URLLoader 	= new URLLoader(new URLRequest(url));
			loader.dataFormat 		= URLLoaderDataFormat.TEXT;
			
			loader.addEventListener(Event.COMPLETE, onLoadComplete);	
		}
		private function onLoadComplete(e:Event):void
		{
			var data:XML = XML((e.target as URLLoader).data);
			parseFromData(data);
			//if(onParseComplete != null) onParseComplete();
		}
		public function parseFromData(data:XML):void
		{
			default xml namespace = "http://www.collada.org/2005/11/COLLADASchema";
			
			var zIsUp:Boolean = (data.asset.up_axis == "Z_UP");
			var library_geometries:XMLList = data..library_geometries;
			var num_geometries:int = library_geometries.children().length();
			
			for each (var library_geometry:XML in library_geometries.children())
			{
				var mesh:Mesh3D = new Mesh3D();
				var name:String = library_geometry.@name;
				
				var dic:Dictionary = new Dictionary();
				
				var sourceList:XMLList = library_geometry..source;
				var floatArray:XMLList = library_geometry..float_array;
				
				var numSources:int = sourceList.length();
				
				for(var i:int = 0; i < numSources; i++)
				{
					var source:XML = sourceList[i];
					var float_array:XML = source.float_array[0];
					var jobby:String = float_array.toString().split("\n").join(" ");
					var bufferArray:Array = jobby.split(" ");
					var buffer:Vector.<Number> = Vector.<Number>(bufferArray);
//					dic["#"+source.@id] = buffer;
					dic[i] = buffer;
				}
				
				var triangleList:XML = library_geometry..triangles[0];
				var inputs:XMLList = triangleList.input;
				var triangles:XML = triangleList.p[0];
				
				var ids:Vector.<uint> = Vector.<uint>(triangles.toString().split(" "));
				/*
				var vertId:String = inputs.(@semantic == "VERTEX")[0].@source;
				mesh.vertices = dic[vertId];
				trace(vertId);
				trace(mesh.vertices);*/
				mesh.vertices = dic[0];
				mesh.normals = dic[1];
				mesh.uvs = dic[2];
			}
			var nuts:String = "w00t";
			
		}
	}
}
import flash.geom.Matrix3D;

class MeshData
{
	public var name:String;
	public var ids			:Vector.<uint>;
	public var vertices		:Vector.<Number>;
	public var normals		:Vector.<Number>;
	public var uvs			:Vector.<Number>;
	public var matrix		:Matrix3D;
}